<?php
namespace Content\Controller;
use Think\Controller;
use Think\Model\RelationModel;

class ObjController extends Controller 
{   
  protected $articleObj;
  protected $typeObj;
  protected $articleImageObj;
  protected $imageObj;
  protected $articleAttributeObj;
  protected $attributeObj;
  protected $slideObj;
  protected $navObj;
  protected $status;

  public function _initialize()
  {      
      $this->articleObj = M("Article");
      $this->articleDetailObj = M("Article_detail");
      $this->typeObj = D("Content/Type");
      $this->imageObj = M("Image");
      $this->articleImageObj = M("Article_image");
      $this->articleAttributeObj = M("Article_attribute");
      $this->attributeObj = M("Attribute");
      $this->slideObj = M("Slide");
      $this->navObj = M("Nav");
      $this->status = array('gt',0);
  }
  public function getTypeidByArticleId($articleId)
  {
    $articleObj = $this->articleObj->field('typeid')->find($articleId);
    if (!empty($articleObj)) {
      $typeId=$articleObj['typeid'];
      if ($typeId>0) {
          $types = $this->getTypeByTypeId($typeId);
          return $types[0]['id'];
      }
    }
    return null;
  }
  public function getArticleById($id=0)
	{
	  $article = $this->articleObj->find($id);
	  return $article;
	}
  public function getArticleByIdArr($ids=0)
  {
    $condition['id']=array("in",$ids);
    $article = $this->articleObj->where($condition)->select();
    return $article;
  }
  public function getArticleDetailById($artilceId=0)
  {
    $article_detail = $this->articleDetailObj->where("articleid=%d",$artilceId)->find();
    return $article_detail['content'];
  }
  
  public function getArticleTitleById($artilceId=0)
  {
    $article_titleObj = $this->articleObj->field('id,title')->find($artilceId);
    return $article_titleObj['title'];
  }
  public function getArticleByTypeId($typeId=0)
  {
    $articles = $this->articleObj->where("typeId=%d",$typeId)->select();
    return $articles;
  }
  public function getArticleByCondition($field='id,title', $where=array(), $order=array(),$limit=15)
  {
    $articles = $this->articleObj->field($field)->where($where)->order($order)->limit('0,'.$limit)->select();
    return $articles;
  }
  public function getArticleListByParentId($parentId=0)
  {
    $articles = $this->articleObj->where("parentId=%d",$parentId)->select();
    return $articles;
  }
  public function getArticleListByArticleIdTypeId($articleId=0,$typeId=0)
  {
    $condition['id']=array("not in",$articleId);
    $condition['typeid'] = $typeId;
    $condition['status'] = $this->status;
    $articles = $this->articleObj->where($condition)->select();
    return $articles;
  }
  public function getFrontImageByArtileId($articleId=0)
  {
    $Model = M();
    $frontImage = $Model->query("select a.*,b.* from __ARTICLE_IMAGE__  a left join  __IMAGE__ b on a.imageid = b.id where a.articleid=$articleId");
    $frontImage = (!empty($frontImage)) ? $frontImage[0] : null ;
    return $frontImage;
  }
  public function getImageByImageId($imageId=0)
  {
    $image=$this->imageObj->find($imageId);
    return $image;
  }
  public function getArticleImageByArtileId($articleId=0)
  {
    $articleImages = $this->articleImageObj->where("articleId=%d",$articleId)->order(array('listorder'=>'Asc','id'=>'asc'))->select();
    return $articleImages;
  }
  public function getAttributeByMark($articleId=0,$mark=0)
  {
    $Model = M(); 
    $attributeValue = $Model->query("select a.value from  __ARTICLE_ATTRIBUTE__  a left join __ATTRIBUTE__ b  on a.attributeid = b.id where a.articleid=$articleId and b.mark='$mark'");
    $attributeValue = (!empty($attributeValue)) ? $attributeValue[0]['value'] : null ;
    return $attributeValue;
  }
  public function getSlides()
  {
    $where["status"]= $this->status;
    $slides = $this->slideObj->where($where)->order(array('listorder'=>'Asc','id'=>'asc'))->select();
    return $slides;
  }
  public function getTypeByTypeId($typeId=0)
  {
    $type = $this->typeObj->find($typeId);
    if(!empty($type)){
      $parentid=$type['parentid'];
      if ($parentid>0) {
        $types = $this->typeTrees($parentid,1);
        //$level=count($types);
        $types[0]=$type;
      }else{
        $types[0]=$type;
      }
      sort($types);
      return $types;
    }else{
      return null;
    }
  }
  public function typeTrees($parentid=0,$level=0)
  {
    if ($parentid>0) {
      static $tree = array();
        $type = $this->typeObj->find($parentid);
        if (!empty($type)) 
        {
          $parentId = $type['parentid'];
          $tree[$level] = $type;
          $this->typeTrees($parentId,$level+1);
        }
        return $tree;
    }else{
      return $tree;
    }
        
  }
  /*根据 栏目父Id 获取子栏目*/
  public function getChildTypeByParentId($parentId=0)
  {
    $parentType = $this->typeObj->find($parentId);
    $types[0]=array('id'=>$parentId,'name'=>$parentType["name"]);
    $childTypes = $this->typeObj->where('parentid=%d',$parentId)->select();
    $types =(!empty($childTypes)) ? array_merge($types,$childTypes):$types;
    return $types;
  }
  public function getParentTypeIdByTypeId($typeId=0)
  {
     $type=$this->typeObj->field('id,parentid')->find($typeId);
    if (!empty($type)){
        $parentTypeId=$type['parentid'];
        if ($parentTypeId>0) {
            return $parentTypeId;
        }else{
            return $type['id'];
        }
    }else{
        return 0;
    }
  }

  public function getParentTypeIdByArticleIdOrUrl($articleParam=0)
  {
    if (is_numeric($articleParam)){
          $where['id']=$articleParam;
    }else{
          $where['url']=$articleParam;
    }
    $article=$this->articleObj->field('id,typeid')->where($where)->find();
    if (!empty($article)) {
        return $this->getParentTypeIdByTypeIdOrUrl($article['typeid']);
    }else{
      return 0 ;
    }
  }

  public function getTypeById($typeId)
  {
    $type = $this->typeObj->find($typeId);
    return $type;
  }

  
  public function getTypeUrlById($typeId)
  {
    $typeObj = $this->typeObj->field('id,url')->find($typeId);
    if (!empty($typeObj)) {
        $typeUrl = $typeObj['url'];
        $typeId = $typeObj['id'];
        $url=(empty($typeUrl)) ? $typeId : $typeUrl;
        $url="/category/".$url.".html";
    }else{
        $url = "/404.html";
    }
    return $url;
  }
/*获取navtree*/
  public function navTree($parentid=0,$level=0,$html='|── ')
  {
    static $tree = array();
    $navList = $this->getNavList($parentid);
    if (!empty($navList)) 
    {
        foreach($navList as $key => $nav)
        {
            $nav['sort'] = $level;
            $html=str_repeat($html,$level);
            $nav['html'] = $html;
            $tree[]= $nav;
            $parentId = $nav['id'];
            $this->navTree($parentId,$level+1);
        }
    }
    return $tree;
  }

  public function getNavList($parentId)
  {
      $where["parentid"] = $parentId;
      $navList = $this->navObj->where($where)->order(array("listorder" => "ASC"))->select();
      return $navList;
  }
}
?>  